package com.nbstar.film.spider.excel;

import com.nbstar.film.spider.domain.FilmSpiderDomain;
import com.nbstar.film.spider.pojo.RawFromExcel;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

public abstract class XSSFExcelReader implements IExcelReader {
    private static Logger logger = LoggerFactory.getLogger(XSSFExcelReader.class);

    private LinkedBlockingQueue<FilmSpiderDomain>  filmSpiderQueue = new LinkedBlockingQueue<FilmSpiderDomain>();

    public LinkedBlockingQueue<FilmSpiderDomain> getFilmSpiderQueue() {
        return filmSpiderQueue;
    }

    @Override
    public void setFilmSpiderQueue(LinkedBlockingQueue<FilmSpiderDomain> filmSpiderQueue) {
        this.filmSpiderQueue = filmSpiderQueue;
    }

    protected  String getCellValue(XSSFRow row, int index) {
        XSSFCell cell = row.getCell(index);
        if (null == cell) {
            return "";
        }
        if (cell.getCellType() == CellType.STRING) {
            return cell.getStringCellValue();
        }

        if (cell.getCellType() == CellType.NUMERIC) {
            return cell.getNumericCellValue() + "";
        }

        return null;
    }

    public abstract   RawFromExcel readRowFromExcel(XSSFRow row) ;

    @Override
    public LinkedBlockingQueue<FilmSpiderDomain> loadExcel(String modelPath, int sheetIndex, int startRow) throws IOException {
        //获取所读取excel模板的对象
        XSSFWorkbook wb = null;
        XSSFSheet sheet = null;
        LinkedBlockingQueue<FilmSpiderDomain> results = new LinkedBlockingQueue<>();

        wb = new XSSFWorkbook(new FileInputStream(modelPath));
        sheet = wb.getSheetAt(sheetIndex);
        int rowNum = sheet.getLastRowNum();
        logger.info("获取到行号：{}", rowNum);
        for (int i = startRow; i < rowNum+1; i++) {

            XSSFRow row = sheet.getRow(i);
            // 获取行里面的总列数
            int columnNum = 0;
            if (row != null) {
                columnNum = row.getPhysicalNumberOfCells();
            }

            if (columnNum > 0) {
                try {
                    //String cellValue = cell.getRawValue();//.getStringCellValue();
                    RawFromExcel rawFromExcel = readRowFromExcel(row);

                    if (!StringUtils.isEmpty(rawFromExcel.getFilmName())) {
                        FilmSpiderDomain spiderDomain = new FilmSpiderDomain();
                        spiderDomain.setRaw(rawFromExcel);
                        results.add(spiderDomain);
                        filmSpiderQueue.add(spiderDomain);
                    }
                } catch (Exception e) {
                    logger.error("Found error at {}, {}" ,i, e.getMessage());
                }
            }
        }

        return filmSpiderQueue;
    }
}
